/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool.map;

import java.util.List;

/**
 * @author Young Ken.
 */
public class MapAlgorithm extends MapConstant {

//    @Deprecated
//    public static double oldArea(Geometry geometry) {
//
//        if (geometry == null || geometry.getCoordinates().length == 0) {
//            return 0;
//        }
//
//        double latLength = 20037508.3427892 * 2 * Math.cos(
// geometry.getCoordinates()[0].y * (2 * Math.PI / 360)) / 360;
//        double longLength = 20037508.3427892 / 180;
//        return geometry.getArea() * latLength * longLength;
//    }
//
//    /**
//     * 面积计算
//     * @param coordinates
//     * @return
//     */
//    public static double area(Coordinate[] coordinates) {
//        int len = coordinates.length;
//        Coordinate coordinate;
//        List list = new ArrayList();
//        for (int i = 0; i < len; i++) {
//            coordinate = coordinates[i];
//            double[] xy = new double[2];
//            xy[0] = coordinate.x;
//            xy[1] = coordinate.y;
//            list.add(xy);
//        }
//        return area(list);
//    }
//
//    public static double area(  Geometry geometry) {
//
//        double sumArea=0;
//        if(geometry instanceof Polygon) {
//            Polygon  p =(Polygon)geometry;
//            sumArea += area( p.getExteriorRing().getCoordinates());
//
//            if(p.getNumInteriorRing()>0){
//                for(int i=0;i<p.getNumInteriorRing();i++){
//                    sumArea +=area( p.getInteriorRingN(i).getCoordinates());
//                }
//            }
//
//        }else if(geometry instanceof MultiPolygon){
//            MultiPolygon multiPolygon=(MultiPolygon)geometry;
//            int k= multiPolygon.getNumGeometries();
//            for(int i=0;i<k;i++){
//                Polygon  p =(Polygon) multiPolygon.getGeometryN(i);
//                sumArea += area(p.getExteriorRing().getCoordinates());
//                if(p.getNumInteriorRing()>0){
//                    for(int j=0;j<p.getNumInteriorRing();j++){
//                        sumArea += area( p.getInteriorRingN(j).getCoordinates());
//                    }
//                }
//            }
//
//        }
//
//        return sumArea;
//    }

    /**
     * 面积计算
     *
     * @param coordinates List double
     * @return double
     */
    public static double area(List<double[]> coordinates) {
        double area = 0;
        int len = coordinates.size();
        if (len < 3) {
            return 0;
        }
        double x1 = coordinates.get(len - 1)[0];
        double y1 = coordinates.get(len - 1)[1];
        for (int i = 0; i < len; i++) {
            double x2 = coordinates.get(i)[0];
            double y2 = coordinates.get(i)[1];
            area += toRadians(x2 - x1) * (2 + Math.sin(toRadians(y1)) + Math.sin(toRadians(y2)));
            x1 = x2;
            y1 = y2;
        }
        return Math.abs(area * EARTH_RADIUS * EARTH_RADIUS / 2.0);
    }

    /**
     * to radians
     *
     * @param angleInDegrees angleInDegrees
     * @return radians
     */
    public static double toRadians(double angleInDegrees) {

        return angleInDegrees * Math.PI / 180;
    }
//
//    public static double perimeter(Geometry geometry) {
//
//        if (geometry == null || geometry.getCoordinates().length == 0) {
//            return 0;
//        }
//
//        double perimeter = 0.0;
//        Coordinate[] coordinates = geometry.getCoordinates();
//        for (int i = 0; i < coordinates.length - 1; i++) {
//
//            perimeter += distanceForLatitudeLongitude(coordinates[i], coordinates[i + 1]);
//        }
//        return perimeter;
//    }

//    public static double distanceForLatitudeLongitude(
// double startLang, double startLat, double endLang, double endLat) {
//        double lat1 = toRadians(startLat);
//        double lat2 = toRadians(endLat);
//        double deltaLatBy2 = (lat2 - lat1) / 2;
//        double deltaLonBy2 = toRadians(endLang - startLang) / 2;
//        double a = Math.sin(deltaLatBy2) * Math.sin(deltaLatBy2) +
//                Math.sin(deltaLonBy2) * Math.sin(deltaLonBy2) *
//                        Math.cos(lat1) * Math.cos(lat2);
//        return 2 * EARTH_RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
//    }

//    public static double distanceForLatitudeLongitude(Coordinate start, Coordinate end) {
//        double lat1 = toRadians(start.y);
//        double lat2 = toRadians(end.y);
//        double deltaLatBy2 = (lat2 - lat1) / 2;
//        double deltaLonBy2 = toRadians(end.x - start.x) / 2;
//        double a = Math.sin(deltaLatBy2) * Math.sin(deltaLatBy2) +
//                Math.sin(deltaLonBy2) * Math.sin(deltaLonBy2) *
//                        Math.cos(lat1) * Math.cos(lat2);
//        return 2 * EARTH_RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
//    }
}
